{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b4a637fc",
   "metadata": {},
   "source": [
    "# python基础入门2_数据结构\n",
    "\n",
    "\n",
    "\n",
    "Python中的数据结构主要包括列表、元组、字符串、字典和集合等。\n",
    "\n",
    "列表（list）：是最常用的数据类型，可以对任意对象进行操作，如添加、删除和修改。同时，列表是可变的，即可以修改和扩充。此外，列表还可以当作堆栈或队列使用。\n",
    "\n",
    "元组（tuple）：与列表类似，但是元组是不可变的，即不能修改和扩充。不过，元组可以当作列表使用。\n",
    "\n",
    "字符串（string）：也是一种不可变的数据类型，不能修改和扩充。字符串是由字符组成的序列，可以当作列表使用。\n",
    "\n",
    "集合(set)：是另一种可变的数据类型，可以插入和删除。集合是由唯一元素组成的无序集合，可以进行集合运算和判断。例如，可以使用交集、并集、差集等运算。\n",
    "以上这些数据结构都有各自的方法和用法，需要根据实际编程需求来选择和使用。\n",
    "\n",
    "字典(dict)：是一种可变的数据类型，可以插入和删除。字典是由键值对组成的无序集合，每个键对应一个值，通过键来访问值。字典的实现是Python的标准库中的dict，可以通过花括号、字典表达式和字典解析式来创建和操作。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17b6fc96",
   "metadata": {},
   "source": [
    "## 列表\n",
    "列表（list）：是最常用的数据类型，可以对任意对象进行操作，如添加、删除和修改。同时，列表是可变的，即可以修改和扩充。此外，列表还可以当作堆栈或队列使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0ff73c32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b2ec04db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1,2,3,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e2eb0077",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5bc230ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ce5b7ffd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__add__',\n",
       " '__class__',\n",
       " '__class_getitem__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__delitem__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__iadd__',\n",
       " '__imul__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__mul__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__reversed__',\n",
       " '__rmul__',\n",
       " '__setattr__',\n",
       " '__setitem__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'append',\n",
       " 'clear',\n",
       " 'copy',\n",
       " 'count',\n",
       " 'extend',\n",
       " 'index',\n",
       " 'insert',\n",
       " 'pop',\n",
       " 'remove',\n",
       " 'reverse',\n",
       " 'sort']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(lst)\n",
    "#Python函数命名规范\n",
    "\n",
    "#abc：前后无下划线表示该变量、函数、成员活方法是公用的，可以以任何形式访问。\n",
    "\n",
    "#_abc：前面单下划线表示半保护，被看作“protect”，只有类对象和子类对象自己能访问这些变量，\n",
    "#      模块或类外不能使用，不用使用 ‘from module import *’导入。\n",
    "\n",
    "#__abc：全私有，全保护，私有成员”private\"，只有类对象自己能访问，连子类对象也不能访问，更不能使用‘from module import *’导入。\n",
    "\n",
    "#__abc__：内建方法，不建议用户这样定义，这种双下划线的命名方式用于python里面的特殊方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b4a69249",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2323b87a",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b8cfa85d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "cd858c77",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a04e2241",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "61f09996",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5,6,7,8,1,2,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "2c88fabd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst.count(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "70875dfa",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst=[1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "5e837323",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst.extend([4,5,6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "76a006dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c6770ce2",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst=[1,2,3]\n",
    "lst.append([4,5,6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "57310aed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6]]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "bcc76212",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'int' object is not iterable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_2388/3969866030.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mlst\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mlst\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'int' object is not iterable"
     ]
    }
   ],
   "source": [
    "lst=[1]\n",
    "lst.extend(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "768199b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst=[1]\n",
    "lst.append(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "b6d136ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "4e48371d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst=[1,2,3,4,5,6,7,8,9,1,2,3]\n",
    "lst.index(1,0,3)#index from 0 to (3-1)   #for(i=start;i<end;i++ )  =>  i from start to end-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "3d9b44b1",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "2 is not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_2388/3053346801.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mlst\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: 2 is not in list"
     ]
    }
   ],
   "source": [
    "lst.index(2,3,9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "a570537b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst.index(2,3,12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "9fb71ad1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 7, 2, 3]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst=[1,2,3]\n",
    "lst.insert(1,7)\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "09d0afc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 7, 2]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst.pop()\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "6de1d19c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst.pop(1)\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "6c002b87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst=[1,2,3]\n",
    "lst.remove(2)#pop是按位置弹出。remove按值弹出\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "680690ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 2, 1]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst=[1,2,3]\n",
    "lst.reverse()\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "13e02c72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst.sort()#默认升序\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "4c031a3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 2, 1]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst.sort(reverse=True)#降序\n",
    "lst"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d772dee",
   "metadata": {},
   "source": [
    "## 元组\n",
    "元组（tuple）：与列表类似，但是元组是不可变的，即不能修改和扩充。不过，元组可以当作列表使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "42e47019",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "26adba86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2)"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "ce349a80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type((1,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "f3beb145",
   "metadata": {},
   "outputs": [],
   "source": [
    "# const list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "b1040d4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "tup = (1,2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "52e59f86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(tup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "eec9fc20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__add__',\n",
       " '__class__',\n",
       " '__class_getitem__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__getnewargs__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__mul__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__rmul__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'count',\n",
       " 'index']"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(tup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "aa0b3297",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3)"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "09561077",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup.count(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "d0795903",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup.index(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "602a87a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "t=(1,2,(1,2))#元组嵌套"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "17e781d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, (1, 2))"
      ]
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16026edb",
   "metadata": {},
   "source": [
    "## 字符串\n",
    "字符串（string）：也是一种不可变的数据类型，不能修改和扩充。字符串是由字符组成的序列，可以当作列表使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "42158976",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "daf9f490",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "f5abddb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mystr=\"hello\"\n",
    "type(mystr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "02ae6a98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__add__',\n",
       " '__class__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__getnewargs__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__mod__',\n",
       " '__mul__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__rmod__',\n",
       " '__rmul__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'capitalize',\n",
       " 'casefold',\n",
       " 'center',\n",
       " 'count',\n",
       " 'encode',\n",
       " 'endswith',\n",
       " 'expandtabs',\n",
       " 'find',\n",
       " 'format',\n",
       " 'format_map',\n",
       " 'index',\n",
       " 'isalnum',\n",
       " 'isalpha',\n",
       " 'isascii',\n",
       " 'isdecimal',\n",
       " 'isdigit',\n",
       " 'isidentifier',\n",
       " 'islower',\n",
       " 'isnumeric',\n",
       " 'isprintable',\n",
       " 'isspace',\n",
       " 'istitle',\n",
       " 'isupper',\n",
       " 'join',\n",
       " 'ljust',\n",
       " 'lower',\n",
       " 'lstrip',\n",
       " 'maketrans',\n",
       " 'partition',\n",
       " 'removeprefix',\n",
       " 'removesuffix',\n",
       " 'replace',\n",
       " 'rfind',\n",
       " 'rindex',\n",
       " 'rjust',\n",
       " 'rpartition',\n",
       " 'rsplit',\n",
       " 'rstrip',\n",
       " 'split',\n",
       " 'splitlines',\n",
       " 'startswith',\n",
       " 'strip',\n",
       " 'swapcase',\n",
       " 'title',\n",
       " 'translate',\n",
       " 'upper',\n",
       " 'zfill']"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(mystr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "de7c8850",
   "metadata": {},
   "outputs": [],
   "source": [
    "str=\"Hello World!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "112ab8ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str.find('H')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "466f4896",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str.find(' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "4cac19dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str.istitle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "eac5a3b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str=\"hello world!\"\n",
    "str.istitle()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3a25590",
   "metadata": {},
   "source": [
    "## 集合\n",
    "集合(set)：是另一种可变的数据类型，可以插入和删除。集合是由唯一元素组成的无序集合，可以进行集合运算和判断。例如，可以使用交集、并集、差集等运算。 以上这些数据结构都有各自的方法和用法，需要根据实际编程需求来选择和使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "19e1c4a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{1,2,3}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "6023ba81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type({1,2,3})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "a5e11d39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type({})#{}默认表示空字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "5ac746e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(set({}))#{}转换成集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "ffabf15c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__and__',\n",
       " '__class__',\n",
       " '__class_getitem__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__iand__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__ior__',\n",
       " '__isub__',\n",
       " '__iter__',\n",
       " '__ixor__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__or__',\n",
       " '__rand__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__ror__',\n",
       " '__rsub__',\n",
       " '__rxor__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__sub__',\n",
       " '__subclasshook__',\n",
       " '__xor__',\n",
       " 'add',\n",
       " 'clear',\n",
       " 'copy',\n",
       " 'difference',\n",
       " 'difference_update',\n",
       " 'discard',\n",
       " 'intersection',\n",
       " 'intersection_update',\n",
       " 'isdisjoint',\n",
       " 'issubset',\n",
       " 'issuperset',\n",
       " 'pop',\n",
       " 'remove',\n",
       " 'symmetric_difference',\n",
       " 'symmetric_difference_update',\n",
       " 'union',\n",
       " 'update']"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(set())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "a72f32fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "set1=set({1,2,3,4,4,4,4})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "b92b339c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4}"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "45f4541f",
   "metadata": {},
   "outputs": [],
   "source": [
    "set2=set({1,1,2,2,5,6,7})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "bb558358",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 5, 6, 7}"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "9c14322d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2}"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1.intersection(set2)#求交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "011ad3de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{3, 4}"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1.difference(set2)#求差集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "cf068584",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{5, 6, 7}"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set2.difference(set1)#求差集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "b71a402c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7}"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set2.union(set1)#求并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "d76ab116",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1.issubset(set2)#判断子集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "545e39dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "set1={1,2,3,4}\n",
    "set2={1,2,5,6}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "3b25d5c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2}"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1.intersection(set2)#求交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "cd34d7d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2}"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1.intersection_update(set2)#求交集并修改原集合\n",
    "set1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "03757eb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4}"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1={1,2,3}\n",
    "set1.add(4)\n",
    "set1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "id": "162bf9bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1.clear()\n",
    "set1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39532a89",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "81c4b6b9",
   "metadata": {},
   "source": [
    "## 字典\n",
    "字典(dict)：是一种可变的数据类型，可以插入和删除。字典是由键值对组成的无序集合，每个键对应一个值，通过键来访问值。字典的实现是Python的标准库中的dict，可以通过花括号、字典表达式和字典解析式来创建和操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "fd7c0f03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type({})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "4044aca9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#字典和集合的区别：\n",
    "#字典的每个item是一个键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "e325923b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'jack', 'age': 12, 'sex': 'M'}"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{\"name\":\"jack\",\"age\":12,\"sex\":\"M\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4e82c38f",
   "metadata": {},
   "outputs": [],
   "source": [
    "dic={\"name\":\"jack\",\"age\":12,\"sex\":\"M\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "id": "08ed074c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'jack', 'age': 12, 'sex': 'M'}"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "1aaaf400",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__class__',\n",
       " '__class_getitem__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__delitem__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__ior__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__or__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__reversed__',\n",
       " '__ror__',\n",
       " '__setattr__',\n",
       " '__setitem__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'clear',\n",
       " 'copy',\n",
       " 'fromkeys',\n",
       " 'get',\n",
       " 'items',\n",
       " 'keys',\n",
       " 'pop',\n",
       " 'popitem',\n",
       " 'setdefault',\n",
       " 'update',\n",
       " 'values']"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(dic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "a80f7f31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['name', 'age', 'sex'])"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "id": "80975af3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['jack', 12, 'M'])"
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "f5d15241",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('name', 'jack'), ('age', 12), ('sex', 'M')])"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "3c5cc24a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'jack'"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic[\"name\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "29868086",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic[\"age\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "f0d72c62",
   "metadata": {},
   "outputs": [],
   "source": [
    "dic[\"age\"]=18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "315b3bfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'jack', 'age': 18, 'sex': 'M'}"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "id": "3ca19464",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name\n",
      "age\n",
      "sex\n"
     ]
    }
   ],
   "source": [
    "for k in dic:\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "id": "02bedbd2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name\n",
      "age\n",
      "sex\n"
     ]
    }
   ],
   "source": [
    "for k in dic.keys():\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "1be1a028",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jack\n",
      "18\n",
      "M\n"
     ]
    }
   ],
   "source": [
    "for v in dic.values():\n",
    "    print(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "id": "2270fa9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('name', 'jack')\n",
      "('age', 18)\n",
      "('sex', 'M')\n"
     ]
    }
   ],
   "source": [
    "for i in dic.items():\n",
    "    print(i)#tuple 可以当list使用，but不能修改增删里面的item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "be581a61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name jack\n",
      "age 12\n",
      "sex M\n"
     ]
    }
   ],
   "source": [
    "for k,v in dic.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "id": "2589dacb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'jack', 'age': 18, 'sex': 'M'}"
      ]
     },
     "execution_count": 196,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字典嵌套\n",
    "dic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "id": "7c221cb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "dics={\"00001\":dic}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "id": "9fe33483",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'00001': {'name': 'jack', 'age': 18, 'sex': 'M'}}"
      ]
     },
     "execution_count": 198,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "id": "aa28b08a",
   "metadata": {},
   "outputs": [],
   "source": [
    "dic={\n",
    "    'name': 'jack',\n",
    "    'age': 18,\n",
    "    'sex': 'M'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "id": "51051c05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'00001': {'name': 'jack', 'age': 18, 'sex': 'M'}}"
      ]
     },
     "execution_count": 200,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "id": "f1a76968",
   "metadata": {},
   "outputs": [],
   "source": [
    "dic1={\n",
    "    'name': 'bob',\n",
    "    'age': 18,\n",
    "    'sex': 'M'\n",
    "}\n",
    "dics[\"00002\"]=dic1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "id": "daf7114f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'00001': {'name': 'jack', 'age': 18, 'sex': 'M'},\n",
       " '00002': {'name': 'bob', 'age': 18, 'sex': 'M'}}"
      ]
     },
     "execution_count": 202,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "id": "b7821f72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'jack', 'age': 18, 'sex': 'M'}\n",
      "{'name': 'bob', 'age': 18, 'sex': 'M'}\n"
     ]
    }
   ],
   "source": [
    "for i in  dics.items():\n",
    "    print(i[1])#根据tuple的下标，只输出dic信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4241a5fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for k,v in  dics.items():\n",
    "    print()#根据tuple的下标，只输出dic信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "404372bc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
